﻿using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.Properties;
using DevelopTool.Model;
using DevelopTool.Model.CommonModels;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace DevelopTool.Forms.DocumentForms
{
    /// <summary>
    /// CMD命令手册
    /// </summary>
    public partial class CmdCommandForm : BaseUIPage
    {
        /// <summary>
        /// DataGridView列信息集合
        /// </summary>
        private List<DataGridColumn> dataGridColumns = new List<DataGridColumn>()
              {
                { new DataGridColumn("Command"){ColumnText="命令",Width=200,ColumnType=ColumnType.DefaultFilter|ColumnType.RowMain}},
                { new DataGridColumn("Description"){ColumnText="描述",Width=0,ColumnType=ColumnType.DefaultFilter}},
                { new DataGridColumn("Example"){ColumnText="示例",Width=-1,ColumnType = ColumnType.DefaultFilter|ColumnType.LongText,ShowFormProperty=new ShowFormProperty(){
                BaseNameColumn="Command",TextType="Batch"
                }}},
              };

        public CmdCommandForm()
        {
            InitializeComponent();
        }


        private void CmdCommandForm_Load(object sender,EventArgs e)
        {
            //将Json转为DataTable 并根据Command列去重
            DataTable dataTable = ZipHelper.GetContent(Resources.CmdCommand_Json).JsonToDataTable().AsEnumerable()
                      .GroupBy(row => new
                      {
                          Column1 = row.Field<string>("Command")
                      })
                      .Select(group => group.First())
                      .CopyToDataTable();

            this.dataDisplayControl.BindData(dataTable, this.dataGridColumns, "Command ASC");

        }

        private void 运行CMD命令ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            string command = this.dataDisplayControl.SelectedRowDic["Command"];

            FileHelper.OpenRunCmd(command);
        }

        private void dataDisplayControl_DataGridViewCellClick(DataGridViewEventArgs dataGridViewEventArgs)
        {
            this.dataDisplayControl.ShowCodeEditor(dataGridViewEventArgs.Row,dataGridViewEventArgs.Row.Cells["Example"],dataGridColumns.Find(it => it.ColumnName == "Example"));

            dataGridViewEventArgs.Cancel = true;
        }
    }
}
